package com.dfrz.stockMarket.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dfrz.stockMarket.bean.MessageBean;
import com.dfrz.stockMarket.model.StockLogModel;

public class StockLogDao {
	 /*  public static void main(String[] args) throws Exception {
		 getStockLogByID(2);
	}*/
	public static MessageBean getStockLogByID(int ID) throws Exception {
		List<StockLogModel> stockLog = new ArrayList<StockLogModel>();
		StockLogModel stockLogModel = new StockLogModel();
		
		//加载驱动
				Class.forName("oracle.jdbc.driver.OracleDriver");
				System.out.println("加载成功");
				//获取连接
				Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE", "hr", "hr");
				System.out.println("连接成功");
				//�?��事务
				conn.setAutoCommit(false);
				
				//获取stmt
				PreparedStatement stmt = conn.prepareStatement("select stocklogid, orderid, stockcode, type, stockquantity, stockprice, to_char(createdate,'yyyy-mm-dd hh24:mi:ss:ff') createdate1 from sto_stocklog where stocklogid=?");
				stmt.setInt(1, ID);

				ResultSet rs = stmt.executeQuery();
				while(rs.next()){
					int stocklogid = rs.getInt("stocklogid");
					int orderid = rs.getInt("orderid");
					int stockcode = rs.getInt("stockcode");
					int type = rs.getInt("type");
					int stockquantity = rs.getInt("stockquantity");
					int stockprice = rs.getInt("stockprice");
					String createdate1 = rs.getString("createdate1");
					  Date date = new Date();  
			        //注意format的格式要与日期String的格式相匹配  
			        DateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");  
			        try {  
			            date = sdf.parse(createdate1);  
			        } catch (Exception e) {  
			            e.printStackTrace();  
			        } 
					/*System.out.println(date);*/
					 
					
					stockLogModel.setCreateDate(date);
					stockLogModel.setOrderID(orderid);
					stockLogModel.setStockCode(stockcode);
					stockLogModel.setStockLogID(stocklogid);
					stockLogModel.setStockPrice(stockprice);
					stockLogModel.setType(type);
					stockLogModel.setStockQuantity(stockquantity);
					
					stockLog.add(stockLogModel);
				}
				

				/*System.out.println(stockLog.size());*/
				//提交or回滚
				conn.commit();
//				conn.rollback();
				//释放stmt conn
				rs.close();
				stmt.close();
				conn.close();
	
		return new MessageBean(true, stockLog);
	}
	
	
}
